using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 堆垛机过弯 : MonoBehaviour
{
    public 立库_堆垛机 _立库_堆垛机;
    public event Action start_delgate;
    public Action stop_delgate;
    public Action endAnimator_delgate;

    public Animation 过弯_Animation;//Animation无法获取，已改为Animator
    public Animator 过弯_animator;
    string animitionName = "堆垛机过弯";
    public float speed;
    void Start()
    {

    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            PlayAnimtion_positive();
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            PlayAnimtion_negative();
        }
    }

    public void PlayAnimtion_positive()//正播
    {
        过弯_Animation[animitionName].time = 0;
        过弯_Animation[animitionName].speed = speed;
        过弯_Animation.CrossFade(animitionName);
        过弯_Animation.Play(animitionName);
    }

    public void PlayAnimtion_negative()//倒播
    {
        过弯_Animation[animitionName].time = 过弯_Animation[animitionName].clip.length;
        过弯_Animation[animitionName].speed = -speed;
        过弯_Animation.CrossFade(animitionName);
        过弯_Animation.Play(animitionName);
    }

    public void PlayAnimator_positive()//正播
    {
        过弯_animator.CrossFade(animitionName, 0.1f);
        过弯_animator.speed = 1;
    }

    public void PlayAnimator_negative() //倒播
    {
        过弯_animator.CrossFade(animitionName, 0.1f);
        过弯_animator.speed = -1;
    }

    public void StartAnimator() //动画起始点
    {
        if (过弯_animator.speed == 1)
        {
            start_delgate?.Invoke();
            StartAnimator_过弯();
        }
        else if (过弯_animator.speed == -1)//速度为-1则结束
        {
            stop_delgate?.Invoke();
            EndAnimator_过弯();
        }
    }

    public void StopAnimator()//动画终点
    {
            Debug.Log("终点："+ 过弯_animator.speed);
        if (过弯_animator.speed == 1)//速度为1则结束
        {
            EndAnimator_过弯();
        }
        else if (过弯_animator.speed == -1)
        {
            start_delgate?.Invoke();
            StartAnimator_过弯();
        }
    }

    /// 过弯动画开始
    void StartAnimator_过弯() 
    {
        _立库_堆垛机.StartAnimetor过弯_Funtion();
    }

    //过弯动画完成
    void EndAnimator_过弯()
    {
        endAnimator_delgate?.Invoke();
        _立库_堆垛机.EndAnimetor过弯_Funtion();
        过弯_animator.speed = 0;
    }

    public void Close_delgate_Start()
    {
    }

    public void Close_delgate_Stop()
    {

    }
}
